﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using CodeStash.Common.DataAccess.EntityFramework;

namespace CodeStash.Models.Snippet
{
    public class EditSnippetViewModel : ISnippetViewModel
    {
        #region Ctor
        /// <summary>
        /// For Post request where ModelBinding takes care of matching up properties for us
        /// </summary>
        public EditSnippetViewModel()
        {
            LanguageList = new List<Language>();
            VisibilityList = new List<Visibility>();

            
        }
        #endregion


        public void PopulateFromExistingSnippet(CodeSnippet snippet, IEnumerable<CodeTag> tags)
        {
            CodeSnippetId = snippet.CodeSnippetId;
            LanguageId = snippet.LanguageId;
            CodeCategoryId = snippet.CodeCategoryId;
            if (snippet.CodeCategoryId > 0)
            {
                NewCodeCategoryName = snippet.CodeCategory.CodeCategoryName.Trim();
            }
            if (snippet.GroupId.HasValue)
            {
                GroupId = snippet.GroupId.Value;
                NewGroupingName = snippet.Grouping.Description.Trim();
            }
            if (tags.Any())
            {
                string tagsData = "";
                foreach (CodeTag tag in tags)
                {
                    tagsData += string.Format("{0};",tag.Description.Trim());
                }
                if (tagsData.EndsWith(";"))
                    tagsData = tagsData.Substring(0, tagsData.Length - 1);

                Tags = tagsData;
            }

            Description = snippet.Description.Trim();
            Title = snippet.Title.Trim();
            ActualCode = snippet.ActualCode.Trim();
            Visibility = snippet.Visibility;
            AspNetMembershipUserId = snippet.AspNetMembershipUserId;

        }

        #region Public Properties

        public int CodeSnippetId { get; set; }

        public List<Language> LanguageList { get; set; }

        [Required(ErrorMessage = "You must enter a value for Language")]
        public int LanguageId { get; set; }

        public int CodeCategoryId { get; set; }


        [Required(ErrorMessage = "You must enter a value for Language")]
        public string NewCodeCategoryName { get; set; }


        public int GroupId { get; set; }


        [StringLength(100, MinimumLength = 0,
            ErrorMessage = "Grouping must be between 0-100 characters in length")]
        public string NewGroupingName { get; set; }


        [StringLength(100, MinimumLength=0,
            ErrorMessage = "Tags must be between 0-100 characters in length")]
        public string Tags { get; set; }


        [Required(ErrorMessage = "You must enter a value for Description")]
        public string Description { get; set; }


        [Required(ErrorMessage = "You must enter a value for Title")]
        public string Title { get; set; }


        [Required(ErrorMessage = "You must enter a value for ActualCode")]
        public string ActualCode { get; set; }


        public List<Visibility> VisibilityList { get; set; }

        [Required(ErrorMessage = "You must enter a value for Visibility")]
        public int Visibility { get; set; }
        

        public Guid AspNetMembershipUserId { get; set; }



        #endregion
    }

}